package com.datamation.swadeshi.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.ListViewDataAdapter;
import com.datamation.swadeshi.adapter.MergeAdapter;
import com.datamation.swadeshi.model.ContentItem;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.github.mikephil.charting.utils.YLabels;

@SuppressLint("NewApi") 
public class FragmentSalesManagement  extends Fragment {
	
	public FragmentSalesManagement(){}
	
	 ArrayList<ContentItem> objects;
		
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.sales_mangement, container, false);
        //set title 
        createCutomActionBarTitle("Sales Menagement");
		// nav drawer icons from resources
        TypedArray icons = getResources().obtainTypedArray(R.array.listView_icons); 
       
        objects = new ArrayList<ContentItem>();
        MergeAdapter mergeAdapter = new MergeAdapter();
        
        ArrayList<BarData> list = new ArrayList<BarData>();
        list.add(generateData(1));
        ChartDataAdapter chartAdapter = new ChartDataAdapter(getActivity().getApplicationContext(), list);
        
        objects.add(new ContentItem("Pre Sales", "",icons.getResourceId(0, -1)));
        objects.add(new ContentItem("Van Sales", "",icons.getResourceId(1, -1)));
        objects.add(new ContentItem("Productive Call", "",icons.getResourceId(2, -1)));
        objects.add(new ContentItem("Non Productive Call", "",icons.getResourceId(3, -1)));
        
        ListViewDataAdapter adapter = new ListViewDataAdapter(getActivity().getApplicationContext(), objects);
        
        ListView lv = (ListView) rootView.findViewById(R.id.listView1);
        
        mergeAdapter.addAdapter(adapter);
        mergeAdapter.addAdapter(chartAdapter);
        lv.setAdapter(mergeAdapter);
        
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view2,
					int position, long id) {
				
				
				switch (position) {
				case 0:
					Toast.makeText(getActivity(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 1:
					Toast.makeText(getActivity(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 2:
					Toast.makeText(getActivity(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 3:
					Toast.makeText(getActivity(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;

				}

			}
		});
        return rootView;
    }
	
	
	/*
	 * chart
	 *
	 */
	
    private class ChartDataAdapter extends ArrayAdapter<BarData> {

        private Typeface mTf;

        public ChartDataAdapter(Context context, List<BarData> objects) {
            super(context, 0, objects);

            mTf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/OpenSans-Regular.ttf");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            BarData c = getItem(position);

            ViewHolder holder = null;

            if (convertView == null) {

                holder = new ViewHolder();

                convertView = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item_barchart, null);
                holder.chart = (BarChart) convertView.findViewById(R.id.chart);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // apply styling
            holder.chart.setValueTypeface(mTf);
            holder.chart.setDescription("");
            holder.chart.setDrawVerticalGrid(false);
            holder.chart.setDrawGridBackground(false);
            holder.chart.setValueTextColor(Color.WHITE);

            XLabels xl = holder.chart.getXLabels();
            xl.setCenterXLabelText(true);
            xl.setPosition(XLabelPosition.BOTTOM);
            xl.setTypeface(mTf);
            
            YLabels yl = holder.chart.getYLabels();
            yl.setTypeface(mTf);
            yl.setLabelCount(5);

            // set data
            holder.chart.setData(c);
            
            // do not forget to refresh the chart
//            holder.chart.invalidate();
            holder.chart.animateY(700);

            return convertView;
        }

        private class ViewHolder {

            BarChart chart;
        }
    }
    /**
     * generates a random ChartData object with just one DataSet
     * 
     * @return
     */
    private BarData generateData(int cnt) {

        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

        for (int i = 0; i < 12; i++) {
            entries.add(new BarEntry((int) (Math.random() * 70) + 30, i));
        }

        BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);    
        d.setBarSpacePercent(20f);
        d.setColors(ColorTemplate.VORDIPLOM_COLORS);
        d.setBarShadowColor(Color.rgb(203, 203, 203));
        
        ArrayList<BarDataSet> sets = new ArrayList<BarDataSet>();
        sets.add(d);
        
        BarData cd = new BarData(getMonths(), sets);
        return cd;
    }

    private ArrayList<String> getMonths() {

        ArrayList<String> m = new ArrayList<String>();
        m.add("Jan");
        m.add("Feb");
        m.add("Mar");
        m.add("Apr");
        m.add("May");
        m.add("Jun");
        m.add("Jul");
        m.add("Aug");
        m.add("Sep");
        m.add("Okt");
        m.add("Nov");
        m.add("Dec");

        return m;
    }
	
    //Title name method
    private void createCutomActionBarTitle(String title){
        
    	getActivity().getActionBar().setDisplayShowCustomEnabled(true);
    	getActivity().getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = LayoutInflater.from(getActivity());
        View v = inflator.inflate(R.layout.custom_action_bar, null);

        //Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/fat_tats.ttf");
        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/coopbl.ttf");
        
        TextView tvTitle = (TextView)v.findViewById(R.id.titleFragment1);
        tvTitle.setText(title);
        tvTitle.setTypeface(tf);
      
        //assign the view to the actionbar
        getActivity().getActionBar().setCustomView(v);
    }
    

    
    
}
